#include <stdio.h>
#include <string.h>
#include "litS.h"
 
void ajouter()
{
	int ic;
	FILE* fl;
	char chemin[]="litS.bin";
	FILE *f5;
	char chemin5[]="nbrlitS.txt";
	

   fl=fopen(chemin,"ab");

	if (fl==NULL)
    		printf ("\nFichier inexistant\n");
	else
	{
     		lit unLit;

     		printf ("\n -Reference:\t");
     		fflush (stdin);
     		scanf("%d",&unLit.Reference);
  		do 
  		{
    			printf ("\n -type du lit :(adulte ou enfant) \t");
    			scanf ("%s",unLit.type);
   		}
		while (strcmp (unLit.type,"adulte") && strcmp(unLit.type,"enfant"));

		do 
		{
			printf ("\n -etat:Ridelle dans le lit?:( oui ou non ) \t");
			scanf ("%s",unLit.etat);
		}
		while (strcmp (unLit.etat,"oui")&& strcmp(unLit.etat,"non"));

									
		printf ("\n -date de livraision: \t");
		do 
		{
			printf ("\n -l'année: \t");
			scanf ("%d",&unLit.dateLivraison.a);
		}
		while (unLit.dateLivraison.a<2014);

		do
 		{
			printf ("\n -le mois: \t");
			scanf ("%d",&unLit.dateLivraison.m);
		}
		while (unLit.dateLivraison.m>12);


		do
 		{
			printf ("\n -le jour: \t");
			scanf ("%d", &unLit.dateLivraison.j);
		}

		while (unLit.dateLivraison.j>31);
 		
		fwrite (&unLit,sizeof(lit),1,fl);
		

		
		f5=fopen(chemin5,"r");
		
			if (f5==NULL)
				printf ("\nFichier inexsistant \n");
		        else {
			
    			fscanf(f5,"%d",&ic);
    			ic++;
   			fclose(f5);


		f5=fopen(chemin5,"w");
			fprintf(f5,"%d",ic);
			fclose (f5);
   		     }

		fclose (fl);
		}
}



void affiche_liste()
{		FILE *f;
		char chemin[]="litS.bin";
		lit t;
		f=fopen(chemin,"rb");

     	if(f!=NULL)
	{
		printf("\n--- liste des lits ---:\n");
		printf("\n-------------------------\n");
		

			while(fread(&t,sizeof(lit),1,f)!=0);
			{
			printf ("\n %d \n",t.Reference);
    			printf ("\n %s \n",t.etat);
			printf ("\n %s \n",t.type);
			printf("\n %d %d %d \n",t.dateLivraison.j,t.dateLivraison.m,t.dateLivraison.a);
			}
	}
fclose(f);
}








void supprimer (int codeSupp)
{
     int ic;
     FILE *ancienF;
     FILE *nouveauF;
     char chemin[]= "litS.bin";
     char chemin2[]="fintermed.bin"; 
     char chemin5[]="nbrlitS.txt";
     ancienF=fopen(chemin,"rb");
     nouveauF=fopen(chemin2,"wb");

     lit litSupp;

     while (fread(&litSupp,sizeof(lit),1,ancienF)!=0)
{
        if (litSupp.Reference!=codeSupp)
              
            fwrite (&litSupp,sizeof(lit),1,nouveauF);


       	else 
           {
      	FILE *f5;
     	char chemin[]="nbrlitS.txt";
     		
	f5=fopen(chemin5,"r");
      
      		if (f5==NULL)
   			   
      		         printf ("\nFichier inexsistant\n");
     			       
           

       	        else 
 	       {
            fscanf(f5,"%d",&ic);
            ic--;
            fclose (f5);
   		

	 f5=fopen(chemin5,"w");
			fprintf(f5,"%d",ic);
			fclose (f5);
                }
            }
}

       fclose (nouveauF);
       fclose (ancienF);
    remove("litS.bin");
    rename("fintermed.bin","litS.bin");
}







int nbractuel ()

{
			int ic ;
			FILE *f;
			char chemin []="nbrlitS.txt";
			f=fopen(chemin,"r");
	if (f==NULL)
		{
		printf ("\nFichier inexsistant\n");
		
		return 0;
		}
	else
		{ 
		fscanf(f,"%d",&ic);
		fclose(f);
		return ic;
		}
} 




